//
//  HLItemCourseView.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/5.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLItemCourseView.h"
#import "HLCourse.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface HLItemCourseView()

/** ImageView  */
@property (nonatomic ,strong) UIImageView *iconView;

/** title  */
@property (nonatomic ,strong) UILabel *title;
/** seeNum  */
@property (nonatomic ,strong) UILabel *seeNum;

@end

@implementation HLItemCourseView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
        [self addTapGesture];
    }
    return self;
}

- (UILabel *)categaryTitle
{
    if (!_categaryTitle ) {
        _categaryTitle = [[UILabel alloc] init];
        _categaryTitle.textAlignment = NSTextAlignmentCenter;
        _categaryTitle.backgroundColor = kColorMineBlueBG;
        _categaryTitle.textColor = [UIColor whiteColor];
        _categaryTitle.font = [UIFont boldSystemFontOfSize:IS_IPHONE_5 ? 11: 13];
    }
    return _categaryTitle;
}

- (UIImageView *)iconView
{
    if (!_iconView ) {
        _iconView = [[UIImageView alloc] init];
        _iconView.layer.cornerRadius = 3;
        _iconView.clipsToBounds = YES;
        
    }
    return _iconView;
}

- (UILabel *)title
{
    if (!_title ) {
        _title = [[UILabel alloc] init];
        _title.backgroundColor = [UIColor whiteColor];
        _title.textColor = HLRGB(60, 60, 60);
        _title.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12: kMainTitleFontSize];
    }
    return _title;
}

- (UILabel *)seeNum
{
    if (!_seeNum) {
        _seeNum = [[UILabel alloc]init];
        _seeNum.backgroundColor = [UIColor whiteColor];
        _seeNum.textColor = HLRGB(120, 120, 120);
        _seeNum.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 11: 14];
    }
    return _seeNum;
}

- (void)setupUI
{
    [self addSubview:self.iconView];
    [self.iconView addSubview:self.categaryTitle];
    [self addSubview:self.title];
    [self addSubview:self.seeNum];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(@(self.height *0.7));
    }];
    
    [self.categaryTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(16));
        make.top.equalTo(self.iconView);
        make.right.equalTo(self.iconView);
        make.width.equalTo(@(35));
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(self.height * 0.15));
        make.top.equalTo(self.iconView.mas_bottom);
        make.right.equalTo(self.iconView);
        make.width.equalTo(self);
    }];
    
    [self.seeNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(self.height * 0.12));
        make.top.equalTo(self.title.mas_bottom);
        make.right.equalTo(self.iconView);
        make.width.equalTo(self);
    }];
    
    
}

- (void)addTapGesture
{
    UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc]initWithTarget:self action:@selector(currentItemDidClick)];
    [self addGestureRecognizer:tap];
    self.userInteractionEnabled = YES;
}

- (void)currentItemDidClick
{
    if ([_delegate respondsToSelector:@selector(itemCourseView:itemDidClick:)]) {
        [_delegate itemCourseView:self itemDidClick:self.course];
    }
}

- (void)setCourse:(HLCourse *)course
{
    _course = course;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:course.book_img] placeholderImage:nil];
    self.title.text = course.book_name;
    NSString *numStr = course.num.length == 0 ? @"0":course.num;
    self.seeNum.text = [NSString stringWithFormat:@"%@人观看",numStr];
    self.categaryTitle.text = course.course_type == 0 ? @"视频":@"文章";
    
}


@end
